package com.wjw.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjw.pojo.Reader;
import com.wjw.pojo.User;
import com.wjw.service.ReaderService;
import com.wjw.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private ReaderService readerService;

    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    @RequestMapping("toLogin")
    public String toLogin() {
        return "index";
    }

    //登录
    @RequestMapping("login")
    public String login(String userName, String userPwd, Model model, HttpSession session) {
        if (userName.equals("admin")) {
            boolean i = userService.login(userName, userPwd);
            if (i) {
                QueryWrapper<User> qw = new QueryWrapper<>();
                qw.eq("username", userName);
                User user = userService.getOne(qw);
                session.setAttribute("currentUser", userName);
                session.setAttribute("password", user.getPassword());
                session.setAttribute("image", user.getImage());
                session.setAttribute("email", user.getEmail());
                return "waterMainMenu";
            } else {
                model.addAttribute("msg", "用户名或密码错误!");
                return "index";
            }
        } else {
            boolean i = readerService.login(userName, userPwd);
            if (i) {
                QueryWrapper<Reader>qw= new QueryWrapper<>();
                qw.eq("rname",userName);
                Reader reader = readerService.getOne(qw);
                session.setAttribute("currentUser", userName);
                session.setAttribute("password", reader.getPassword());
                session.setAttribute("image", reader.getRimage());
                session.setAttribute("email", reader.getEmail());
                session.setAttribute("userId", reader.getId());
                return "waterMainMenu1";
            } else {
                model.addAttribute("msg", "用户名或密码错误!");
                return "index";
            }
        }
    }

    @RequestMapping("count")
    public String count() {
        return "chart";
    }

    //修改密码
    @RequestMapping("pwd")
    public String preUpdate() {
        return "modify";
    }

    @PostMapping("pwdUser")
    public String updatePwd(String userPwd, String newPwd, Model model, HttpSession session) {
        String currentUser = (String) session.getAttribute("currentUser");
        boolean login = userService.login(currentUser, userPwd);
        if (login) {
            User user = new User();
            user.setUsername(currentUser);
            String newPassword = DigestUtil.md5Hex(newPwd);
            user.setPassword(newPassword);
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("username", user.getUsername());
            boolean b = userService.update(user, qw);
            if (b) {
                return "index";
            } else {
                model.addAttribute("loginFail", "修改密码失败");
            }
        } else {
            model.addAttribute("loginFail", "用户验证失败");
        }
        return "modify";
    }

    //退出登录
    @RequestMapping("logout")
    public String logout() {
        return "index";
    }

    //注册
    @RequestMapping("/register")
    public String register(String userName, String userPwd,String confirmPwd, Model model, HttpSession session) {

        if (!userPwd.equals(confirmPwd)){
            model.addAttribute("msg", "输入密码不一致");
            return "register";
        }else {
            Reader reader = new Reader();
            reader.setRname(userName);
            String s = DigestUtil.md5Hex(userPwd);
            reader.setPassword(s);
            readerService.save(reader);
            return "index";
        }
    }

    //个人简介
    @RequestMapping("profile")
    public String profile(HttpServletRequest request, Model model){
        String currentUser = (String) request.getSession().getAttribute("currentUser");
        String password =(String) request.getSession().getAttribute("password");
        if (currentUser.equals("admin")){
            QueryWrapper<User>qw=new QueryWrapper<>();
            qw.eq("username",currentUser);
            User one = userService.getOne(qw);
            one.setPassword(password);//为了在个人简介页面显示的是没加密的密码
            model.addAttribute("user",one);
            return "profile-admin";
        }else {
            QueryWrapper<Reader>qw=new QueryWrapper<>();
            qw.eq("rname",currentUser);
            Reader one = readerService.getOne(qw);
            one.setPassword(password);
            model.addAttribute("user",one);
            return "profile-reader";
        }

    }

    //修改管理员个人信息
    @RequestMapping("updateAdminProfile")
    public String updateProfile(User user){
        //修改数据库
        String s = DigestUtil.md5Hex(user.getPassword());
        user.setPassword(s);
        boolean b = userService.updateById(user);
        return "index";
    }
    //修改读者个人信息
    @RequestMapping("updateReaderProfile")
    public String updateReaderProfile(Reader reader){
        //修改数据库
        String s = DigestUtil.md5Hex(reader.getPassword());
        reader.setPassword(s);
        boolean b = readerService.updateById(reader);
        return "index";
    }

}
